﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.MGPInterface
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Thread mythread;
        private Socket socket;


        private void Form1_Load(object sender, EventArgs e)
        {
            Common.Log4.LogManage.WriteInfo("winform set in the constructor");
            try
            {
                this.mythread = new Thread(new ThreadStart(this.BeginListen));
                this.mythread.Start();
                base.Hide();
                // this.Hide();
                //this.SetLocalTime();
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        private void BeginListen()
        {
            try
            {
                bool flag;
                IPEndPoint localEP = new IPEndPoint(GetServerIP(), 8212);
                this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.socket.Blocking = false;
                this.socket.Bind(localEP);
                goto Label_0094;
            Label_003E:;
                try
                {
                    this.socket.Listen(5);
                    Thread.Sleep(1000);
                    byte[] buffer = new byte[100];
                    this.socket.Accept().Receive(buffer);
                    string strInfo = Encoding.GetEncoding("GB2312").GetString(buffer);
                    SendKeys.SendWait(strInfo);
                    Common.Log4.LogManage.WriteInfo("BeginListen接收数据" + strInfo);
                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr(ex.ToString());
                }
            Label_0094:
                flag = true;
                goto Label_003E;
            }
            catch (ThreadAbortException ex)
            {
                // MessageBox.Show(exception.ExceptionState.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }

        }

        public static IPAddress GetServerIP()
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
            string pattern = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            Regex regex = new Regex(pattern);
            foreach (IPAddress address in hostEntry.AddressList)
            {
                if (regex.IsMatch(address.ToString()))
                {
                    return address;
                }
            }
            return null;
        }

        private void QXtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.Visible = false;
        }

        private void TCtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要关闭运销系统煤管票接口？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.mythread.Abort("运销系统煤管票接口正常关闭");
                base.Close();
            }
        }
    }
}
